
package com.yili.oa.service.impl;

import com.yili.oa.dao.PermissionMapper;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.models.Permission;
import com.yili.oa.models.PermissionExample;
import com.yili.oa.service.PermisService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.vo.request.PermissionDataReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;


/**
 * @author lxr
 * @version v0.01
 * @date 2019/3/27 0027
 * @email 200890949@qq.com
 */

@SuppressWarnings("ALL")
@Service
@Transactional
public class PermisSvImpl implements PermisService {

    @Autowired
    private PermissionMapper mapper;

    @Override
    public Boolean add(PermissionDataReq permissionDataRe) {
        Permission permission = new Permission();
        permission.setPermissionId(UUID.randomUUID().toString());
        permission.setPermissionName(permissionDataRe.getPermissionName());
        permission.setPermissionDecription(permissionDataRe.getPermissionDecription());
        permission.setPermissionKey(permissionDataRe.getPermissionKey());
        try {
            mapper.insert(permission);
        }catch (Exception e){
            throw new AjaxException("出现错误 请重试");
        }
        return true;
    }

    @Override
    public List<Permission> getList(SearchBase searchBase) {
        PermissionExample example =new PermissionExample();
        List<Permission> list = mapper.selectByExample(example);
        return list;
    }

    @Override
    public Permission getById(String id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean update(PermissionDataReq permissionDataRe) {
        Permission permission = mapper.selectByPrimaryKey(permissionDataRe.getPermissionId());
        if (permission==null){
            throw new AjaxException("该权限不存在");
        }
        permission.setPermissionName(permissionDataRe.getPermissionName());
        permission.setPermissionDecription(permissionDataRe.getPermissionDecription());
        permission.setPermissionKey(permissionDataRe.getPermissionKey());
        try {
            mapper.updateByPrimaryKeySelective(permission);
        }catch (Exception e){
            throw new AjaxException("出现错误,请重试");
        }
        return true;
    }

    @Override
    public Boolean del(List<String> permissionIds) {
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andPermissionIdIn(permissionIds);
        try {
            mapper.deleteByExample(example);
        }catch (Exception e){
            throw new AjaxException("出现错误,请重试");
        }
        return true;
    }

    @Override
    public Boolean checkName(String name, String id) {
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andPermissionNameEqualTo(name);
        if (StringUtils.isNoneBlank(id)){
            criteria.andPermissionIdNotEqualTo(id);
        }
        List<Permission> list = mapper.selectByExample(example);
        if (ListUtils.isNotNullOrEmpty(list)){
            return false;
        }
        return true;
    }

    @Override
    public Boolean checkCode(String code, String id) {
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andPermissionKeyEqualTo(code);
        if (StringUtils.isNoneBlank(id)){
            criteria.andPermissionIdNotEqualTo(id);
        }
        List<Permission> list = mapper.selectByExample(example);
        if (ListUtils.isNotNullOrEmpty(list)){
            return false;
        }
        return true;
    }
}

